{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8d85acb3",
   "metadata": {},
   "source": [
    "### 字典的认识（{key:value}）\n",
    "\n",
    "1. [关于字典](https://docs.python.org/zh-cn/3/tutorial/datastructures.html#dictionaries)\n",
    " > 字典在其他语言中被称为“联合内存”/“关联数组”  \n",
    " > 字典由*关键字*索引，它可以是任何不可变类型  \n",
    " > 如果元组直接或间接地包含了可变对象，就不能用作关键字。此外，列表也不能当关键字  \n",
    " > 字典可以理解为*键值对*的集合  \n",
    " \n",
    "2. 字典的重要属性：\n",
    " > 1. 结构：key:value（*键值对*）  \n",
    " > 2. 字典是无序的，我们通过*键*(key)去查找*值*(value)，就好像list用索引(index)查找值一样，只不过字典的“索引”被定义了名字  \n",
    " > 3. 字典对key的定义由一定的要求：key是由“不可变”的数据结构才可以定义，list是不能作为key来定义的  \n",
    " > 4. 字典的key是唯一的，字典的值value是可以不唯一的。（list的值是不唯一的）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9610f958",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表的方式\n",
    "users = [\n",
    "    [\"weikai\", 23, \"IT\", 20000],\n",
    "    [\"qunyao\", 26, \"IT\", 30000]\n",
    "]\n",
    "# 试对比 字典的方式\n",
    "users_dict = {\n",
    "    \"weikai\":[23, \"IT\", 20000],\n",
    "    \"qunyao\":[26, \"IT\", 30000]\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3ccc9326",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['qunyao', 26, 'IT', 30000]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询qunyao的信息：\n",
    "users[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5f8d2c85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[26, 'IT', 30000]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users_dict[\"qunyao\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6991392d",
   "metadata": {},
   "source": [
    " ### 字典的创建\n",
    " 1. 一旦数据庞大，我们无法得知数据的index索引值，但是知道数据的关键信息的时候，就可以使用dict进行查询\n",
    " 2. **常用的三种方式**\n",
    " > 1. info = {\"key1\":\"value1\", \"key2\":\"value2\", \"key3\":\"value3\", ...}直接按照字典的格式创建\n",
    " > 2. info_02 = dict(key1=\"value1\", key2=\"value2\", ...) 注意引号的区别\n",
    " > 3. info_03 = {} info_03[\"key1\"] = value1, info_03[\"key2\"] = value2 ... 运用字典增加的方式新建（这个方法比较麻烦）\n",
    " > 4. dict([(\"key1\", value1), (\"key2\", value2), ...])可以直接用键值对序列创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7aa0a5f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 第一种方法\n",
    "info = {\n",
    "    \"stu10001\":\"xiaoming\",\n",
    "    \"stu10002\":\"lihua\",\n",
    "    \"stu10003\":\"xiaohong\",\n",
    "    #\"stu10004\":\"xiaohong\", 值是可以重复出现的\n",
    "    #\"stu10001\":\"qunyao\" 这时候，qunyao会代替xiaoming出现\n",
    "}\n",
    "print(info)\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a583a157",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 第二种方法\n",
    "info_02 = dict(\n",
    "    stu10001 = \"xiaoming\",\n",
    "    stu10002 = \"lihua\",\n",
    "    stu10003 = \"xiaohong\"\n",
    ")\n",
    "print(info_02)\n",
    "print(type(info_02))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "26878536",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'xiaofang', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 第三种方法\n",
    "info_03 = {}\n",
    "info_03[\"stu10001\"] = \"xiaoming\"\n",
    "info_03[\"stu10002\"] = \"xiaofang\"\n",
    "info_03[\"stu10003\"] = \"xiaohong\"\n",
    "print(info_03)\n",
    "print(type(info_03))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "66e70297",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'sape': 4139, 'guido': 4127, 'jack': 4098}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第四种方法\n",
    "dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77f8bda0",
   "metadata": {},
   "source": [
    "### 字典的增、删、改、查\n",
    " 1. 字典的“增”（与字典创建的第三种方法一致）\n",
    "  > info_03 = {} info_03[\"key1\"] = value1, info_03[\"key2\"] = value2 ...\n",
    " 2. 字典的查询\n",
    "  > 1. info_03[key_name]\n",
    "  > 2. 注意：正确的查询方式，应该先查询key是否存在在字典中，如下：\n",
    "  >> 1. key_name in info_03  如果存在则返回结果为Ture，否则为False\n",
    "  >> 2. info_03[key_name]\n",
    " 3. 字典的修改\n",
    "  > 1. 查询key 是否存在\n",
    "  > 2. 修改key的值\n",
    " 4. 字典的删除\n",
    "  > 1. pop(key)\n",
    "  > 2. popitem()\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "41df850f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'xiaofang', 'stu10003': 'xiaohong', 'stu10004': 'xiaoqiang'}\n"
     ]
    }
   ],
   "source": [
    "# 增\n",
    "info_03[\"stu10004\"] = \"xiaoqiang\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "72de8b43",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用key查到value的数据\n",
    "info_03[\"stu10002\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9a370332",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "您查询的key不存在\n"
     ]
    }
   ],
   "source": [
    "# 正确的查询字典的方式\n",
    "print (\"stu10003\" in info_03)\n",
    "if \"stu10006\" in info_03:\n",
    "    print(info_03[\"stu10006\"])\n",
    "else:\n",
    "    print(\"您查询的key不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "4ab1741d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'xiaofang', 'stu10003': 'xiaohong', 'stu10004': 'xiaozhang'}\n"
     ]
    }
   ],
   "source": [
    "# 修改字典的key：当然也需要先查询（养成好习惯\n",
    "if \"stu10004\" in info_03:\n",
    "    info_03[\"stu10004\"] = \"xiaozhang\"\n",
    "else:\n",
    "    print(\"您查询的key不存在\")\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "837b40ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'xiaofang', 'stu10003': 'xiaohong'}\n",
      "{'stu10001': 'xiaoming', 'stu10002': 'xiaofang'}\n"
     ]
    }
   ],
   "source": [
    "# 字典的删除1\n",
    "info_03.pop(\"stu10004\")\n",
    "print(info_03)\n",
    "\n",
    "# 字典的删除2\n",
    "info_03.popitem() # 随机删字典中的其中一个值\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0ebea8a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
